package com.leokok.jts.learning.jts.core.algorithm.distance;

import org.junit.Test;
import org.locationtech.jts.algorithm.distance.DiscreteHausdorffDistance;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

/**
 * 离散豪斯多夫距离计算公式
 *
 * http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/98/normand/main.html
 *
 * https://blog.51cto.com/u_15278213/5340925
 *
 * 计算结果
 *     a, b的距离:    DHD(a, b) <= HD(a, b)
 *
 *  densifyFactor密度参数,通过调整形状点个数来计算距离
 *     DHD(A, B, densifyFactor) -> HD(A, B) as densifyFactor -> 0.0
 *
 */
public class DiscreteHausdorffDistanceTest {

    WKTReader wktReader = new WKTReader();



    /**
     * 计算两个geometry的豪斯多夫距离
     */
    @Test
    public void distanceTest() throws ParseException {

        Geometry g1 = wktReader.read("POLYGON ((330 490, 337 479, 341 467, 346 454, 351 442, 355 431, 361 419, 368 408, 375 393, 385 378, 393 369, 402 361, 412 359, 422 358, 437 358, 454 358, 465 358, 476 360, 487 362, 499 365, 510 367, 520 369, 531 371, 542 374, 552 376, 569 383, 581 388, 591 393, 596 402, 601 415, 604 426, 607 439, 609 453, 610 466, 611 479, 611 489, 611 501, 610 511, 604 520, 597 529, 589 535, 580 540, 567 546, 557 550, 544 554, 532 559, 520 561, 510 563, 500 564, 488 565, 476 567, 466 567, 455 568, 443 568, 432 568, 422 568, 412 568, 402 568, 390 568, 380 569, 370 569, 370 570, 330 490))");
        Geometry g2 = wktReader.read("POLYGON ((590 310, 578 312, 567 310, 554 304, 539 298, 525 291, 512 283, 501 279, 493 272, 484 264, 476 257, 470 248, 465 239, 460 225, 458 214, 456 203, 456 191, 456 179, 456 168, 462 155, 469 147, 480 136, 489 131, 503 124, 519 118, 530 115, 541 111, 553 108, 565 106, 578 104, 592 102, 605 101, 620 101, 636 101, 649 101, 664 101, 676 103, 688 106, 700 109, 710 114, 720 118, 737 128, 750 138, 759 148, 767 158, 774 172, 780 185, 785 198, 787 209, 789 220, 789 230, 789 241, 789 255, 789 266, 785 277, 778 290, 766 301, 754 311, 736 320, 725 323, 713 328, 700 331, 689 334, 679 336, 668 339, 657 341, 647 342, 630 343, 614 344, 603 344, 591 344, 580 344, 571 339, 563 333, 555 327, 550 318, 546 308, 556 308, 568 312, 579 315, 590 318, 600 321, 640 300, 590 310))");

        System.out.println(DiscreteHausdorffDistance.distance(g1,g2,1));

        System.out.println(g2.distance(g1));

    }
}
